﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using Gao.Poker.Domain;

namespace Gao.Poker.App.Print
{
    public class PrintHelper
    {
        private static PrintHelper _instance;
        public static PrintHelper Instance()
        {
            return _instance ?? (_instance = new PrintHelper());
        }

        private Font printFont;

        public List<String> PrintList { get; set; }

        private PrintHelper()
        {
            printFont = new Font("Arial", 10);
            PrintList = new List<string>();
        }

        public void MemberPrint(string memberName, string cardNo,int money, int balance)
        {
            PrintList.Clear();
            
            PrintList.Add(String.Format("会员名 {0}", memberName));
            PrintList.Add(String.Format("卡号 {0}", cardNo));
            PrintList.Add(String.Format("日期 {0}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")));
            PrintList.Add(String.Format("充值积分 {0}", money));
            PrintList.Add(String.Format("剩余积分 {0}", balance));

            Print();
        }

        public void StorePrint(Member member,IList<ProductHelper> list,Decimal monetary)
        {
            PrintList.Clear();

            PrintList.Add(String.Format("会员名 {0}", member.Name));
            PrintList.Add(String.Format("卡号 {0}", member.CardNo));
            PrintList.Add(String.Format("日期 {0}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")));
            foreach (var productHelper in list)
            {
               PrintList.Add(String.Format("{0}---{1}---{2}", productHelper.ProductName, productHelper.PIntegral, productHelper.Number));
            }
            PrintList.Add(String.Format("总计 {0}", monetary));
            PrintList.Add(String.Format("剩余积分 {0}", member.Points));

            Print();
        }

        public void CompetitionPrint(string memberName, string cardNo, string competitionName, string type, string money, string balance, String seatNum)
        {
            PrintList.Clear();

            PrintList.Add(String.Format("会员名 {0}", memberName));
            PrintList.Add(String.Format("卡号 {0}",cardNo));
            PrintList.Add(String.Format("日期 {0}",DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")));
            PrintList.Add(String.Format("比赛 {0}",competitionName));
            PrintList.Add(String.Format("座位号 {0}", seatNum));
            PrintList.Add(String.Format("消费类型 {0}",type));
            PrintList.Add(String.Format("消费积分 {0}",money));
            PrintList.Add(String.Format("剩余积分 {0}",balance));

            Print();
        }

        public void Print()
        {
            PrintDocument pd = new PrintDocument();

            //设置边距
            Margins margin = new Margins(20, 20, 20, 20);
            pd.DefaultPageSettings.Margins = margin;

            ////纸张设置默认
            //PaperSize pageSize = new PaperSize("First custom size", getYc(58), 600);
            //pd.DefaultPageSettings.PaperSize = pageSize;
            pd.PrintPage += new PrintPageEventHandler(PD_PrintPage);
            pd.Print();
        }

        private void PD_PrintPage(object sender, PrintPageEventArgs ev)
        {
            //float linesPerPage = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;

            //// Calculate the number of lines per page.
            //linesPerPage = ev.MarginBounds.Height /
            //   printFont.GetHeight(ev.Graphics);

            // Print each line of the file.
            foreach (var line in PrintList)
            {
                float yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
                count++;
            }
        }

        private int getYc(double cm)
        {
            return (int)(cm / 25.4) * 100;
        }
    }
}
